//
//  CustomTabBar.m
//  Meroad
//
//  Created by 郭孝通 on 2019/1/25.
//  Copyright © 2019 郭孝通. All rights reserved.
//

#import "CustomTabBar.h"

//类扩展
@interface CustomTabBar ()

//类扩展可变数组tabBarItems存放tabBarItem
@property (strong, nonatomic) NSMutableArray *tabBarItems;

@end

@implementation CustomTabBar

#pragma mark - Lifecycle

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    if(self){
        [self config];
    }
    
    return self;
}

#pragma mark - Private Method

-(void)config {
    //tabBar背景设置
    self.backgroundColor = [UIColor whiteColor];
    //tabBar区域顶部上划线设置
    UIImageView *topLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, -12, SCREEN_WIDTH, 12)];
    topLine.image = [UIImage imageNamed:@"topline"];
    [self addSubview:topLine];
}

-(void)setSelectedIndex:(NSInteger)index {
    for (CustomTabBarItem *item in self.tabBarItems){
        if(item.tag == index){
            item.selected = YES;
        }else{
            item.selected = NO;
        }
    }
    
    UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
    UITabBarController *tabBarController = (UITabBarController *)keyWindow.rootViewController;
    if(tabBarController){
        tabBarController.selectedIndex = index;
    }
}

#pragma mark - Touch Event

- (void)itemSelected:(CustomTabBarItem *)sender {
    [self setSelectedIndex:sender.tag];
    [self.delegate tabbarDidSelected:sender.tag];
}

#pragma mark - Setter

-(void)setTabBarItemAttributes:(NSArray<NSDictionary *> *)tabBarItemAttributes {
    _tabBarItemAttributes = tabBarItemAttributes.copy;
    
    CGFloat itemWidth = SCREEN_WIDTH / _tabBarItemAttributes.count;
    CGFloat tabBarHeight = CGRectGetHeight(self.frame);
    NSInteger itemTag = 0;
    BOOL passedRiseItem = NO;
    
    _tabBarItems = [NSMutableArray arrayWithCapacity:_tabBarItemAttributes.count];
    for(id item in _tabBarItemAttributes){
        if([item isKindOfClass:[NSDictionary class]]){
            NSDictionary *itemDict = (NSDictionary *)item;
            
            CustomTabBarItemType type = [itemDict[kCustomTabBarItemAttributeType] integerValue];
            CGRect frame = CGRectMake(itemTag * itemWidth + (passedRiseItem ? itemWidth : 0), 0, itemWidth, tabBarHeight);
            
            CustomTabBarItem *tabBarItem = [self tabBarItemWithFrame:frame
                                                               title:itemDict[kCustomTabBarItemAttributeTitle]
                                                     normalImageName:itemDict[kCustomTabBarItemAttributeNormalImageName]
                                                   selectedImageName:itemDict[kCustomTabBarItemAttributeSelectedImageName]
                                                      tabBarItemType:type];
            if(itemTag == 0){
                tabBarItem.selected = YES;
            }
            
            [tabBarItem addTarget:self action:@selector(itemSelected:) forControlEvents:UIControlEventTouchUpInside];
          
            tabBarItem.tag = itemTag;
            itemTag++;
            
            [_tabBarItems addObject:tabBarItem];
            [self addSubview:tabBarItem];
        }
    }
}

//CustomtabBarItem属性设置函数 
-(CustomTabBarItem *)tabBarItemWithFrame:(CGRect)frame title:(NSString *)title normalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName tabBarItemType:(CustomTabBarItemType)tabBarItemType {
    CustomTabBarItem *item = [[CustomTabBarItem alloc] initWithFrame:frame];
    [item setTitle:title forState:UIControlStateNormal];
    [item setTitle:title forState:UIControlStateSelected];
    if (IS_IPHONE_XS){
        item.titleLabel.font = [UIFont systemFontOfSize:11];
    }else{
        item.titleLabel.font = [UIFont systemFontOfSize:10];
    }
    UIImage *normalImage = [UIImage imageNamed:normalImageName];
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    [item setImage:normalImage
          forState:UIControlStateNormal];
    [item setImage:selectedImage
          forState:UIControlStateSelected];
    [item setImage:selectedImage
          forState:UIControlStateSelected | UIControlStateHighlighted];
    [item setTitleColor:[UIColor colorWithWhite:51 / 255.0 alpha:1]
          forState:UIControlStateNormal];
    [item setTitleColor:[UIColor colorWithRed:100/255.0
                                        green:230/255.0
                                         blue:210/255.0
                                        alpha:1]
          forState:UIControlStateSelected];
    [item setTitleColor:[UIColor colorWithRed:100/255.0
                                        green:230/255.0
                                         blue:210/255.0
                                        alpha:1]
               forState:UIControlStateSelected | UIControlStateHighlighted];
    item.tabBarItemType = tabBarItemType;
    
    return item;
}

//- (void) drawRect:(CGRect)rect {
//    // Drawing code
//    CGSize size = self.bounds.size;
//    CGFloat w = size.width / 2;
//    CGFloat radius = w / 2;
//    CGFloat h = size.height - 49;
//    UIBezierPath *path = [UIBezierPath bezierPath];
//    [path moveToPoint:CGPointMake(0, h)];
//    [path addArcWithCenter:CGPointMake(w, h) radius:radius startAngle:M_PI endAngle:0 clockwise:YES];
//    [path addLineToPoint:CGPointMake(size.width, h)];
//    [path addLineToPoint:CGPointMake(size.width, size.height)];
//    [path addLineToPoint:CGPointMake(0, size.height)];
//    [path addLineToPoint:CGPointMake(0, h)];
//    [path closePath];
//
//    CAShapeLayer *layer = [CAShapeLayer layer];
//    layer.path = path.CGPath;
//    self.layer.mask = layer;
//}

@end
